﻿using CRM.Infrastructure.Dto.Request.Login;
using CRM.Infrastructure.Dto.Response;
using CRM.Infrastructure.Tools;
using CRM.Service.Interface;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace CRM.Api.Admin.Controllers
{
   
    public class LoginController : BaseController
    {
        private readonly I_Login_Service _login_Service;
        private readonly IConfiguration _configuration;

        public LoginController(I_Login_Service login_Service, IConfiguration configuration):base(login_Service)
        {
            _login_Service = login_Service;
            _configuration = configuration;
        }






        /// <summary>
        /// 管理员登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Logins(Login_Request_Dto dto)
        {
            dto.code = Config.GUID2();
            var result=await _login_Service.logins(dto);
            if(result.code == 0)
            {
                Claim[] claims = new Claim[] {
                    new Claim(ClaimTypes.Name,dto.code),
                    new Claim(ClaimTypes.Role,"admin")
                };
                var token = JWTUtil.EnJwt(claims, _configuration["JWT:secretKey"]!, _configuration["JWT:issuer"]!);
                result.data = token;
            }
            return Ok(result);
        }

        [HttpGet]
        public async Task<IActionResult> Check_Login()
        {
            var user = await this.Get_Curr_User();
            return Ok(new Api_Response_Dto
            {
                code = Api_Code.ok,
                message = "ok",
                data = new
                {
                    account = user.Uaccount,
                    name = user.Uname
                }
            });
        }
    }
}
